package com.beiing.tianshuai.tianshuai.widget;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.beiing.tianshuai.tianshuai.util.DensityUtils;
import com.mic.etoast2.Toast;

/**
 * Created by Jinda Yang on 2017/12/25
 * @author Jinda Yang
 */
public class CustomEmptyLayout extends RelativeLayout {

    public interface RetryListener {
        /**
         * 网络请求错误，重试接口
         */
        void retry();
    }

    private ProgressBar progressBar = null;
    private Button btnRefresh = null;
    private TextView tvTip = null;
    private TextView tvEmptyWarning;
    private boolean isNormal = true;
    private RetryListener retryListener = null;

    private final static int TV_TIP_ID = 0x1001;


    public CustomEmptyLayout(Context context) {
        this(context, null);
    }


    public CustomEmptyLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomEmptyLayout(Context context, AttributeSet attrs,
                             int defStyle) {
        super(context, attrs, defStyle);
    }


    /**
     * 设置加载界面
     */
    public void setInProgress() {
        if (progressBar == null) {
            int size = (int) TypedValue
                    .applyDimension(TypedValue.COMPLEX_UNIT_DIP, 45, getResources().getDisplayMetrics());
            progressBar = new ProgressBar(getContext());
            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(size, size);
            lp.addRule(RelativeLayout.CENTER_IN_PARENT);
            progressBar.setLayoutParams(lp);
            addView(progressBar);
        }

        if (getChildCount() > 0) {
            int childCount = getChildCount();
            if (childCount > 0) {
                for (int i = (childCount - 1); i >= 0; i--) {
                    getChildAt(i).setVisibility(View.GONE);
                }
            }
        }

        if (btnRefresh != null) {
            btnRefresh.setVisibility(View.GONE);
        }

        if (tvTip != null) {
            tvTip.setVisibility(View.GONE);
        }

        progressBar.setVisibility(View.VISIBLE);
    }

    /**
     * 显示加载失败界面,隐藏所有正常的View元素
     */
    public void setChildrenGone() {
        if (btnRefresh == null) {
            btnRefresh = new Button(getContext());
            tvTip = new TextView(getContext());
            tvTip.setText("网络连接失败");
            tvTip.setGravity(Gravity.CENTER);
            tvTip.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
            btnRefresh.setText("刷新");

            btnRefresh.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (retryListener != null) {
                        retryListener.retry();
                    } else {
                        Toast.makeText(getContext(), "暂无数据", 0).show();
                    }
                }
            });
            int btnWidth = (int) TypedValue
                    .applyDimension(TypedValue.COMPLEX_UNIT_DIP, 80, getResources().getDisplayMetrics());
            RelativeLayout.LayoutParams lp
                    = new RelativeLayout.LayoutParams(btnWidth, LayoutParams.WRAP_CONTENT);
            lp.addRule(RelativeLayout.CENTER_IN_PARENT);
            lp.setMargins(0, 10, 0, 0);
            btnRefresh.setLayoutParams(lp);
            btnRefresh.setId(TV_TIP_ID);
            btnRefresh.setVisibility(View.GONE);
            addView(btnRefresh);

            RelativeLayout.LayoutParams lpTv
                    = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
            lpTv.addRule(RelativeLayout.ABOVE, TV_TIP_ID);
            tvTip.setLayoutParams(lpTv);
            addView(tvTip);
        }

        if (getChildCount() > 0) {
            int childCount = getChildCount();
            if (childCount > 0) {
                for (int i = (childCount - 1); i >= 0; i--) {
                    getChildAt(i).setVisibility(View.GONE);
                }
            }
        }

        if (progressBar != null) {
            progressBar.setVisibility(View.GONE);
        }

        isNormal = false;
        btnRefresh.setVisibility(View.VISIBLE);
        tvTip.setVisibility(View.VISIBLE);
    }

    /**
     * 显示加载失败界面,隐藏所有正常的View元素
     */
    public void setEmptyView(int layoutId, String text) {
        if (tvEmptyWarning == null) {
            tvEmptyWarning = new TextView(getContext());
            tvEmptyWarning.setText(text);
            tvEmptyWarning.setTextSize(TypedValue.COMPLEX_UNIT_SP, 13);
            tvEmptyWarning.setTextColor(0xFFB2B2B2);
            tvEmptyWarning.setGravity(Gravity.CENTER);
            Drawable drawable = ContextCompat.getDrawable(getContext(), layoutId);
            if (drawable != null) {
                drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
                tvEmptyWarning.setCompoundDrawables(null, drawable, null, null);
                tvEmptyWarning.setCompoundDrawablePadding(DensityUtils.dp2px(getContext(), 40f));
            }
            RelativeLayout.LayoutParams lpTv
                    = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
            lpTv.addRule(RelativeLayout.CENTER_IN_PARENT);
            tvEmptyWarning.setLayoutParams(lpTv);
            addView(tvEmptyWarning);
        }

        if (getChildCount() > 0) {
            int childCount = getChildCount();
            if (childCount > 0) {
                for (int i = (childCount - 1); i >= 0; i--) {
                    getChildAt(i).setVisibility(View.GONE);
                }
            }
        }

        if (progressBar != null) {
            progressBar.setVisibility(View.GONE);
        }

        isNormal = false;
        tvEmptyWarning.setVisibility(View.VISIBLE);
    }

    /**
     * 显示正常View元素
     */
    public void setChildrenVisible() {
        //the number of children in the group
        if (getChildCount() > 0) {
            int childCount = getChildCount();
            if (childCount > 0) {
                for (int i = (childCount - 1); i >= 0; i--) {
                    getChildAt(i).setVisibility(VISIBLE);
                }
            }

            if (progressBar != null) {
                progressBar.setVisibility(GONE);
            }

            if (btnRefresh != null) {
                btnRefresh.setVisibility(GONE);
            }

            if (tvTip != null) {
                tvTip.setVisibility(GONE);
            }

            if (tvEmptyWarning != null) {
                tvEmptyWarning.setVisibility(GONE);
            }

            isNormal = true;
        }
    }

    protected boolean isNormalView() {
        return isNormal;
    }

    public void setRetryListener(RetryListener listener) {
        this.retryListener = listener;
    }
}
